package com.eric.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.eric.domain.dto.ProjectDTO;
import com.eric.domain.po.Project;
import com.eric.domain.vo.ProjectVO;
import com.eric.service.ProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *  控制层。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/project")
@Validated
public class ProjectController {
    private final ProjectService projectService;

//    添加一条新数据
    @PostMapping("save")
    public SaResult save(@Validated @RequestBody ProjectVO projectVO) {
        return projectService.saveOneService(projectVO);
    }

//    根据主键删除一条数据
    @DeleteMapping("remove/{id}")
    public SaResult remove(@PathVariable Long id) {
        if (id == null||id<=0) {
            return SaResult.data("数据错误");
        }
        return SaResult.data(projectService.removeById(id));
    }

//    更新一条数据
    @PutMapping("update")
    public SaResult update(@Validated @RequestBody ProjectDTO projectDTO) {
        return projectService.updateByIdService(projectDTO);
    }

//    分页查询所有数据
    @GetMapping("list")
    public SaResult list(Integer cur, Integer pageSize) {
        return projectService.getListByPageService(cur, pageSize);
    }

//   查询所有数据的数量
    @GetMapping("/allCount")
    public SaResult getAllCount() {
        return SaResult.data(projectService.count());
    }


//   根据id查询一条数据详细信息
    @GetMapping("getInfo/{id}")
    public SaResult getInfo(@PathVariable Long id) {
        Project project = projectService.getById(id);
        if (BeanUtil.isEmpty(project)){
            return SaResult.error("没有数据");
        }
        ProjectDTO projectDTO = BeanUtil.copyProperties(project, ProjectDTO.class);

        return SaResult.data(projectDTO);
    }

//    根据名称查询
    @GetMapping("/getByName")
    public SaResult getByName(String name,Integer cur,Integer pageSize) {
        return projectService.getProjectListByNameService(name, cur, pageSize);
    }

//    根据名称查询到数据的数量
    @GetMapping("/getByNameCount")
    public SaResult getByNameCount(String name) {
        return projectService.getProjectListByNameCountService(name);
    }


}